//
//  UIView+YCExtension.swift
//  yc-basic-common
//
//  Created by 董鹏 on 2022/1/19.
//

import UIKit

/**
 * 代码获取坐标
 */
public extension UIView{
    var left:CGFloat{
        set{
            var frame = self.frame
            frame.origin.x = newValue
            self.frame = frame
        }
        get{
            return self.frame.origin.x
        }
    }
    var top:CGFloat{
        set{
            var frame = self.frame
            frame.origin.y = newValue
            self.frame = frame
        }
        get{
            return self.frame.origin.y
        }
    }
    var right:CGFloat{
        set{
            var frame = self.frame
            frame.origin.x = newValue - frame.size.width
            self.frame = frame
        }
        get{
            return self.frame.origin.x + self.frame.size.width
        }
    }
    var bottom:CGFloat{
        set{
            var frame = self.frame
            frame.origin.y = newValue - frame.size.height
            self.frame = frame
        }
        get{
            return self.frame.origin.y + self.frame.size.height
        }
    }
    var centerX:CGFloat{
        set{
            self.center = CGPoint(x:newValue, y:self.center.x)
        }
        get{
            return self.center.x
        }
    }
    var centerY:CGFloat{
        set{
            self.center = CGPoint(x:self.center.x, y:newValue)
        }
        get{
            return self.center.y
        }
    }
    var width:CGFloat{
        set{
            var frame = self.frame
            frame.size.width = newValue
            self.frame = frame
        }
        get{
            return self.frame.size.width
        }
    }
    var height:CGFloat{
        set{
            var frame = self.frame
            frame.size.height = newValue
            self.frame = frame
        }
        get{
            return self.frame.size.height
        }
    }
    
    /// 给view指定的角切圆角
    /// - Parameters:
    ///   - corners: 切圆角的位置
    ///   - cornerRadi: 切圆角的大小
    ///   - view: 被切圆角的view
    func maskCorners(corners:UIRectCorner, cornerRadi:CGFloat) -> Void {
        self.layer.cornerRadius = cornerRadi
        self.layer.maskedCorners = CACornerMask.init(rawValue: corners.rawValue)
    }
    
    /// 移除所有子视图
    func removeAllSubViews() {
        _ = self.subviews.map{$0.removeFromSuperview()}
    }
    
    /// 转为Image
    func changeToImage(size: CGSize) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
        if let context = UIGraphicsGetCurrentContext() {
            self.layer.render(in: context)
        }
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image ?? UIImage.init()
    }
    
    func showYCToast(message: String) {
        self.makeToast(message, point: self.center, title: nil, image: nil, completion: nil)
    }
    
}
